package chap04.Postfix;

/**
 * Created by root on 2017/7/6.
 */
class StackI{
    private int maxSize;
    private int[] stackArray;
    private int top;

    public StackI(int maxSize) {
        this.maxSize = maxSize;
        stackArray = new int[maxSize];
        top = -1;
    }

    public void push(int j) {
        stackArray[++top] = j;
    }

    public int pop() {
        return stackArray[top--];
    }

    public int peek() {
        return stackArray[top];
    }

    public boolean isEmpty() {
        return (top == -1);
    }

    public boolean isFull() {
        return (top == maxSize-1);
    }

    public  int size() {
        return top+1;
    }

    public int peekN(int n) {
        return stackArray[n];
    }

    public void displayStack(String s) {
        System.out.print(s);
        System.out.print("Stack (bottom-->top): ");
        for (int i=0; i<size(); i++) {
            System.out.print(peekN(i));
            System.out.print(' ');
        }
        System.out.println();
    }
}
class ParsePost {
    private StackI theStack;
    private String input;

    public ParsePost(String s){
        input = s;
    }

    public int doParse() {
        theStack = new StackI(20);
        char ch;
        int j;
        int num1,num2,interAns=0;
        for (j=0; j<input.length(); j++) {
            ch = input.charAt(j);
            theStack.displayStack(" " + ch + " ");
            if (ch >= '0' && ch <= '9') {
                theStack.push((int)ch-'0');
            } else {
                num2 = theStack.pop();
                num1 = theStack.pop();
                switch (ch) {
                    case '+':
                        interAns = num1 + num2;
                        break;
                    case '-':
                        interAns = num1 - num2;
                        break;
                    case '*':
                        interAns = num1 * num2;
                        break;
                    case '/':
                        interAns = num1 / num2;
                        break;
                }
                theStack.push(interAns);
            }
        }
        return theStack.pop();
    }
}
public class PostfixApp {
    public static void main(String[] args) {
//        InToPost theTrans = new InToPost("3+2*5");
//        String output = theTrans.doTrans();
//        ParsePost parsePost = new ParsePost(output);
//        System.out.println(parsePost.doParse());
        char ch = '3';
        int i = (int)ch;
        System.out.println(i);
        System.out.println((int)'0');
    }
}
